package com.example.medicalequipment.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.medicalequipment.R;
import com.example.medicalequipment.entity.Device;

import java.util.List;

public class DeviceListAdapter extends BaseAdapter {

    private Context context;
    private List<Device> deviceList;

    public DeviceListAdapter(Context context, List<Device> deviceList) {
        this.context = context;
        this.deviceList = deviceList;
    }

    @Override
    public int getCount() {
        return deviceList.size();
    }

    @Override
    public Device getItem(int position) {
        return deviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_device_list, parent, false);
            holder = new ViewHolder();
            holder.ivDevice = convertView.findViewById(R.id.ivDevice);
            holder.tvDeviceName = convertView.findViewById(R.id.tvDeviceName);
            holder.tvDepartment = convertView.findViewById(R.id.tvDepartment);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Device device = deviceList.get(position);
        Glide.with(context)
                .load(device.getImageResId())
                .placeholder(R.drawable.devices_1)
                .into(holder.ivDevice);

        holder.tvDeviceName.setText(device.getName());
        holder.tvDepartment.setText(device.getDepartment());

        return convertView;
    }

    private static class ViewHolder {
        ImageView ivDevice;
        TextView tvDeviceName;
        TextView tvDepartment;
    }
}
